using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace WindowsPhoneGame1
{
    class Animation
    {
        protected Texture2D texture;
        protected Point frameSize;
        protected Point sheetSize;
        protected Point currentFrame=new Point();

        protected int deltaTime;
        protected int frameTime;

        public Animation(Texture2D texture,Point frameSize ) {
            this.texture = texture;
            this.frameSize = frameSize;
            this.sheetSize = new Point(texture.Width/frameSize.X,texture.Height/frameSize.Y);
            deltaTime = 50;
            frameTime = 0;
        }

        int DeltaTime {
            get {
                return deltaTime;
            }
            set {
                deltaTime = value;
            }
        }

        public void OnDraw(SpriteBatch spriteBatch,Vector2 position){
            Vector2 pos = position;
          //  pos.X -= texture.Width / 2;
          //  pos.Y -= texture.Height / 2;

            //spriteBatch.Begin();
            spriteBatch.Draw(texture,pos,
                new Rectangle(currentFrame.X*frameSize.X,currentFrame.Y*frameSize.Y,frameSize.X,frameSize.Y),
                Color.White);
            //spriteBatch.End();
        }

        public void Update(int delta)
        {
            frameTime += delta;
            if (frameTime > deltaTime) {
                frameTime -= deltaTime;
                currentFrame.X++;
                if (currentFrame.X >= sheetSize.X)//repeat
                {
                    currentFrame.X = 0;
                    currentFrame.Y++;
                    if (currentFrame.Y >= sheetSize.Y)//repeat
                        currentFrame.Y = 0;
                }
            }
        }
    }
}
